package com.why.dao.hibernate;
// default package

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.why.entity.IndexData;

/**
 	* A data access object (DAO) providing persistence and search support for IndexData entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .IndexData
  * @author MyEclipse Persistence Tools 
 */

public class IndexDataDAO extends HibernateDaoSupport  {
	     private static final Logger log = Logger.getLogger(IndexDataDAO.class);
		//property constants
	public static final String INDEX_ID = "index_id";
	public static final String ORG_ID = "org_id";
	public static final String YEAR = "year";
	public static final String DATA_VALUE = "data_value";
	public static final String DATA_SOURCE = "data_source";
	public static final String OPT_CODE = "opt_code";
	public static final String REMARK = "remark";



	protected void initDao() {
		//do nothing
	}
    
    public void save(IndexData transientInstance) {
        log.debug("saving IndexData instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(IndexData persistentInstance) {
        log.debug("deleting IndexData instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public IndexData findById( java.lang.Integer id) {
        log.debug("getting IndexData instance with id: " + id);
        try {
            IndexData instance = (IndexData) getHibernateTemplate()
                    .get("IndexData", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(IndexData instance) {
        log.debug("finding IndexData instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding IndexData instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from IndexData as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByIndexId(Object indexId
	) {
		return findByProperty(INDEX_ID, indexId
		);
	}
	
	public List findByOrgId(Object orgId
	) {
		return findByProperty(ORG_ID, orgId
		);
	}
	
	public List findByYear(Object year
	) {
		return findByProperty(YEAR, year
		);
	}
	
	public List findByDataValue(Object dataValue
	) {
		return findByProperty(DATA_VALUE, dataValue
		);
	}
	
	public List findByDataSource(Object dataSource
	) {
		return findByProperty(DATA_SOURCE, dataSource
		);
	}
	
	public List findByOptCode(Object optCode
	) {
		return findByProperty(OPT_CODE, optCode
		);
	}
	
	public List findByRemark(Object remark
	) {
		return findByProperty(REMARK, remark
		);
	}
	

	public List findAll() {
		log.debug("finding all IndexData instances");
		try {
			String queryString = "from IndexData";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public IndexData merge(IndexData detachedInstance) {
        log.debug("merging IndexData instance");
        try {
            IndexData result = (IndexData) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(IndexData instance) {
        log.debug("attaching dirty IndexData instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(IndexData instance) {
        log.debug("attaching clean IndexData instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static IndexDataDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (IndexDataDAO) ctx.getBean("IndexDataDAO");
	}
}